<template>
  <!-- 商家销量横向柱状图组件 -->
  <div class="com-container">
    <div class="com-chart" ref="seller"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      // echarts对象
      myChart: null,
      // 销售数据
      sellerData: [],
      // 当前页数
      currentPage: 1,
      // 总页数
      totalPage: 0,
      // 定时器
      timer: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.myChart.dispose()
      this.initChart()
      this.updateChart()
      this.screenAdapter()
    }
  },
  methods: {
    // 初始化echarts对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.seller, this.theme)
      // 图表初始化配置
      const initOption = {
        title: {
          text: '▎商家销售统计',
          textStyle: {
            fontSize: 66
          },
          top: 20,
          left: 20
        },
        grid: {
          top: '20%',
          left: '3%',
          right: '6%',
          bottom: '3%',
          // 以坐标轴文字为边界
          containLabel: true
        },
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            z: 0,
            lineStyle: {
              width: 66,
              color: '#2d3443'
            }
          }
        },
        series: [
          {
            type: 'bar',
            barWidth: 66,
            label: {
              show: true,
              position: 'right',
              textStyle: {
                color: '#fff'
              }
            },
            itemStyle: {
              barBorderRadius: [0, 33, 33, 0],
              // 不同百分比下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#5052EE' },
                { offset: 1, color: '#AB6EE5' }
              ])
            }
          }
        ]
      }
      this.myChart.setOption(initOption)
      // 鼠标移入，定时器关闭
      this.myChart.on('mouseover', () => {
        if (this.timer) clearInterval(this.timer)
      })
      // 鼠标移出，定时器
      this.myChart.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取数据
    async getData() {
      const res = await this.$getApi.getSeller()
      this.sellerData = res
      // 每5个数据显示为1页
      this.totalPage = Math.ceil(res.length / 5)
      this.updateChart()
      this.startInterval()
    },
    // 更新图表
    updateChart() {
      // 每5个数据显示为1页
      const start = (this.currentPage - 1) * 5
      const end = this.currentPage * 5
      const showData = this.sellerData.slice(start, end)
      const sellerName = showData.map(item => {
        return item.name
      })
      const sellerValue = showData.map(item => {
        return item.value
      })
      // 排序，升序
      sellerValue.sort((a, b) => {
        return a - b
      })
      // 图表的数据配置
      const dataOption = {
        yAxis: {
          data: sellerName
        },
        series: [
          {
            data: sellerValue
          }
        ]
      }
      this.myChart.setOption(dataOption)
    },
    // 定时改变页数更新图表
    startInterval() {
      if (this.timer) clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.currentPage++
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1
        }
        this.updateChart()
      }, 3000)
    },
    // 图表适配浏览器窗口大小变化
    screenAdapter() {
      // 这个值是测试得出来的
      const titleFontSize = (this.$refs.seller.offsetWidth / 100) * 3.6
      // 图表的动态样式配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      }
      this.myChart.setOption(adapterOption)
      // 手动调用echarts对象的resize()才会产生效果
      this.myChart.resize()
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    this.screenAdapter()
    window.addEventListener('resize', this.screenAdapter)
  },
  destroyed() {
    if (this.timer) clearInterval(this.timer)
    window.removeEventListener('resize', this.screenAdapter)
  }
}
</script>

<style>
</style>
